package com.lgqz.himalayafm.activity;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.bean.AdvertisementEntity;
import com.lgqz.himalayafm.http.HttpService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class AdvertisementActivity extends AppCompatActivity implements Callback<AdvertisementEntity> {
    private boolean isOnpause;
    private SimpleDraweeView simpleDraweeView;

    private class Mythread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (!isOnpause) {
                Intent intent = new Intent(AdvertisementActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_advertisement);

        simpleDraweeView = (SimpleDraweeView) findViewById(R.id.advertisement_image);

        if (isNetworkAvailable(this)) {

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://adse.ximalaya.com/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
            HttpService service = retrofit.create(HttpService.class);
//        http://adse.ximalaya.com/ting/loading?appid=0&device=android&name=loading&network=wifi&operator=0&version=4.3.98
            service.getAdvertisementData("0", "android", "loading", "wifi", "0", "4.3.98").enqueue(this);
        }
        else {
            Toast.makeText(this,"你没有网，看啥啊",Toast.LENGTH_LONG).show();
            Intent intent = new Intent(AdvertisementActivity.this, MainActivity.class);
            startActivity(intent);
             finish();
        }
    }

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            Log.i("NetWorkState", "Unavailabel");
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        Log.i("NetWorkState", "Availabel");
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isOnpause = true;
    }

    @Override
    public void onResponse
            (Call<AdvertisementEntity> call, Response<AdvertisementEntity> response) {
        AdvertisementEntity advertisementEntity = response.body();
        if (advertisementEntity.getRet().equals("0")) {
            String imagUrl;
            final String webUrl;
            int i = (int) (Math.random() * advertisementEntity.getData().size());
            Log.d("1620", "handleMessage:i " + i);
            imagUrl = advertisementEntity.getData().get(i).getCover();
            webUrl = advertisementEntity.getData().get(i).getLink();
            Log.d("1620", "022222222");
            simpleDraweeView.setImageURI(imagUrl);
            simpleDraweeView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(AdvertisementActivity.this, WebViewActivity.class);
                    intent.putExtra("webUrl", webUrl);
                    startActivity(intent);
                    finish();
                }
            });
        }
        new Mythread().start();
    }

    @Override
    public void onFailure(Call<AdvertisementEntity> call, Throwable t) {

    }
}
